package com.meeruu.sg.account.core.domain.component.thread;


import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Auther: liwu
 * @Date: 2019/5/6 12:03
 * @Description:
 */
public class ThreadExecutorBuilder {


    private static final Map<String, ThreadExecutorTemplate> poolCache = new ConcurrentHashMap<>();

    /**
     * 固定Fixed
     */
    private static final int FIXED = 15;

    /**
     * 异步执行初始核心线程数
     */
    private static final int POOL_SIZE = 10;

    /**
     * 异步执行最大线程数
     */
    private static final int MAX_POOL_SIZE = 30;
    /**
     * 异步执行队列可容纳的最大数
     */
    private static final int QUEUE_SIZE = Integer.MAX_VALUE;


    public static ThreadExecutorTemplate build(String bizType, String threadType) {

        String key = new StringBuffer("").append(bizType).append("_").append(threadType).toString();

        ThreadEnum threadEnum = ThreadEnum.getByValue(threadType);

        switch (threadEnum) {
            case ONE:
                if (null == poolCache.get(key)) {
                    poolCache.put(key, new ThreadExecutorTemplate());
                }
                break;
            case FIXED:
                if (null == poolCache.get(key)) {
                    poolCache.put(key, new ThreadExecutorTemplate(FIXED));
                }
                break;
            case MANUAL:
                if (null == poolCache.get(key)) {
                    poolCache.put(key, new ThreadExecutorTemplate(POOL_SIZE, MAX_POOL_SIZE));
                }
                break;
            case MAN2REJ:
                if (null == poolCache.get(key)) {
                    poolCache.put(key, new ThreadExecutorTemplate(POOL_SIZE, MAX_POOL_SIZE, QUEUE_SIZE));
                }
                break;
        }
        return poolCache.get(key);

    }
}
